﻿using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Jim.Obsidian.Edm;

namespace Jim.Obsidian.Web.Api
{
    public class ApiSubResult
    {

        private string _name;
        private string[] _jProps;
        private string strJProps;
        private List<object[]> valueList = new List<object[]>();

        public string Name
        {
            get { return this._name; }
        }

        public string[] JProps
        {
            get { return this._jProps; }
        }

        internal ApiSubResult(string name, string jprops)
        {
            if (String.IsNullOrEmpty(name))
                throw new Exception("参数name不能为空");
            this._name = name;
            strJProps = jprops;
            this._jProps = jprops.Split(',');
        }

        public void AddValues(params object[] values)
        {
            this.valueList.Add(values);
        }

        public void AddModels<T>(IList<T> modelList) where T : Model
        {
            foreach (T m in modelList)
                this.AddModel(m);
        }

        public void AddModels(params Model[] models)
        {
            foreach (Model m in models)
                this.AddModel(m);
        }

        private void AddModel(Model model)
        {
            object[] values = new object[this._jProps.Length];
            int n = 0;
            foreach (string propName in this._jProps)
            {
                IModelField field = model.GetFieldByJPropName(propName);
                if (field != null)
                    values[n] = field.OValue;
                n++;
            }
            this.valueList.Add(values);
        }

        public string ToJsonString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{\"n\":\"" + this.Name + "\",");
            sb.Append("\"f\":\"" + strJProps + "\",");
            sb.Append("\"v\":");
            JArray jArray = new JArray();
            foreach (object[] vals in valueList)
            {
                JArray jarrValues = new JArray();
                foreach (object val in vals)
                {
                    if (val != null)
                    {
                        string type = val.GetType().Name.ToLower();
                        if (type == "boolean")
                        {
                            bool b = (bool)val;
                            jarrValues.Add(b ? 1 : 0);
                        }
                        else if (type == "datetime")
                        {
                            DateTime dt = (DateTime)val;
                            if (dt == DateTime.MinValue)
                                jarrValues.Add(null);
                            else
                                jarrValues.Add(dt == DateTime.MinValue ? "" : dt.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                        else
                        {
                            jarrValues.Add(val);
                        }
                    }
                    else
                    {
                        jarrValues.Add(val);
                    }
                }
                jArray.Add(jarrValues);
            }
            sb.Append(JsonConvert.SerializeObject(jArray));
            sb.Append("}");
            return sb.ToString();
        }

    }
}
